/**
 * Classname :PagingInfo.java
 *
 * Version information: 1.0
 *
 * Date: Mar 09, 2012 Bui.Trung.Dung create
 *
 */
package com.history.common.domain.db;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author Bui.Trung.Dung	
* Created Mar 09, 2012
*/
public class PagingInfo implements Serializable, Cloneable {
    /**
     * 
     */
    private static final long serialVersionUID = -2128624921508347105L;

    public final static int DEFAULT_INDEX = 1;
    public final static int DEFAULT_OFFSET = 20;
    public final static int DEFAULT_MAX_TOTAL_DISPLAY_PAGE = 10;
    
    private int offset = DEFAULT_OFFSET;
    private long total = 0;
    private int maxtotaldisplaypage = DEFAULT_MAX_TOTAL_DISPLAY_PAGE;
    
    private int indexPage = DEFAULT_INDEX;

    /**
     * 
     */
    public PagingInfo() {
    }

    /**
     * 
     * @param offset
     */
    public PagingInfo(int offset) {
        this.setOffset(offset);
    }

    /**
     * 
     * @param offset
     * @param indexPage
     */
    public PagingInfo(int offset, int indexPage) {
        this.setOffset(offset);
        this.setIndexPage(indexPage);
    }

    /**
     * @return the index
     */
    public int getIndex() {
        return (indexPage - 1) * offset;
    }

    /**
     * @return the offset
     */
    public int getOffset() {
        return offset;
    }

    /**
     * @param offset
     *            the offset to set
     */
    public void setOffset(int offset) {
        if (offset > 0) {
            this.offset = offset;
        }
    }

    /**
     * @return the total
     */
    public long getTotal() {
        return total;
    }

    /**
     * @param total
     *            the total to set
     */
    public void setTotal(long total) {
        this.total = total;
    }

    /**
     * @return the indexPage
     */
    public int getIndexPage() {
        return indexPage;
    }

    /**
     * @param indexPage
     *            the indexPage to set
     */
    public void setIndexPage(int indexPage) {
        if (indexPage > 0) {
            this.indexPage = indexPage;
        }
    }

    /**
     * @return the totalPage
     */
    public long getTotalPage() {
        if (offset == 0) {
            return 0;
        } else {
            long totalPage = (total / offset);
            long divValue = (total % offset);
            return (divValue != 0 ? totalPage + 1 : totalPage);
        }
    }

    /**
     * 
     * @return
     */
    public List<PagingInfoItem> getPagingItems() {
        return generatePagingItems(false);
    }

    /**
     * 
     * @return
     */
    public List<PagingInfoItem> generatePagingItems() {
        return generatePagingItems(true);
    }
    
    /**
     * 
     * @return
     */
    public List<PagingInfoItem> generatePagingItems(boolean descOrder) {
        List<PagingInfoItem> pagingItems = new ArrayList<PagingInfoItem>();
        long totalPage = this.getTotalPage();
        int indexPage = this.getIndexPage();

        // System.out.println("totalPage: " + totalPage);
        // System.out.println("indexPage: " + indexPage);
        if (totalPage > (long)maxtotaldisplaypage) {
            int hIndex = indexPage + maxtotaldisplaypage/2;
            hIndex = (hIndex < totalPage ? hIndex : (int) totalPage);
            if(hIndex <= maxtotaldisplaypage) {
            	hIndex = maxtotaldisplaypage;
            }
            // int lIndex = indexPage - 5 - (5 - (hIndex - indexPage));
            int lIndex = hIndex - maxtotaldisplaypage + 1;
            lIndex = (lIndex < 1 ? 1 : lIndex);

            // System.out.println("hIndex: " + hIndex + ", lIndex:" + lIndex);
            if (descOrder) {
                for (int i = hIndex; i >= lIndex; i--) {
                    pagingItems.add(new PagingInfoItem(i, String.valueOf(i), (i == indexPage)));
                }
            } else {
                for (int i = lIndex; i <= hIndex; i++) {
                    pagingItems.add(new PagingInfoItem(i, String.valueOf(i), (i == indexPage)));
                }
            }

        } else {
            if (descOrder) {
                for (int i = (int) totalPage; i > 0; i--) {
                    pagingItems.add(new PagingInfoItem(i, String.valueOf(i), (i == indexPage)));
                }
            } else {
                for (int i = 1; i <= (int) totalPage; i++) {
                    pagingItems.add(new PagingInfoItem(i, String.valueOf(i), (i == indexPage)));
                }
            }
        }
        return pagingItems;
    }

    public String toString() {
        return "PagingInfo-[index:" + getIndex() + ", offset:" + offset + ", total:" + total + ", indexPage:" + indexPage + ", totalPage:" + getTotalPage() + "]";
    }

    public static void main(String[] args) {
        PagingInfo pagingInfo = new PagingInfo();
        pagingInfo.setTotal(100);

        pagingInfo.setIndexPage(3);
        System.out.println(pagingInfo.generatePagingItems());

        pagingInfo.setIndexPage(10);
        System.out.println(pagingInfo.generatePagingItems());

        pagingInfo.setIndexPage(20);
        System.out.println(pagingInfo.generatePagingItems());

        pagingInfo.setIndexPage(98);
        System.out.println(pagingInfo.generatePagingItems());
    }
}
